package com.share.device.controller;


import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.common.security.utils.SecurityUtils;
import com.share.device.domain.PowerBank;
import com.share.device.service.IPowerBankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 充电宝表 前端控制器
 * </p>
 *
 * @author pww
 * @since 2025-03-24
 */
@RestController
@Tag(name = "充电宝接口管理")
@RequestMapping("/powerBank")
public class PowerBankController extends BaseController {
    @Autowired
    private IPowerBankService powerBankService;


    /**
     * 删除充电宝
     *
     * @param ids
     * @return
     */
    @Operation(summary = "删除充电宝")
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable List<Long> ids) {
        return toAjax(powerBankService.removeBatchByIds(ids));
    }


    /**
     * 更新充电宝
     *
     * @param powerBank
     * @return
     */
    @Operation(summary = "更新充电宝")
    @PutMapping
    public AjaxResult edit(@RequestBody PowerBank powerBank) {
        powerBank.setCreateBy(SecurityUtils.getUsername());
        powerBank.setUpdateTime(LocalDateTime.now());
        return toAjax(powerBankService.updatePowerBank(powerBank));
    }

    /**
     * 新增充电宝
     *
     * @param powerBank
     * @return
     */
    @Operation(summary = "新增充电宝")
    @PostMapping
    public AjaxResult add(@RequestBody PowerBank powerBank) {
        powerBank.setCreateBy(SecurityUtils.getUsername());
        powerBank.setCreateTime(LocalDateTime.now());
        return toAjax(powerBankService.savePowerBank(powerBank));
    }


    /**
     * 根据id查询充电宝详情
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询充电宝详情")
    @GetMapping("/{id}")
    public AjaxResult getIfo(@PathVariable Long id) {
        PowerBank powerBank = powerBankService.getById(id);
        return success(powerBank);
    }

    /**
     * 查询充电宝列表
     *
     * @param powerBank
     * @return
     */
    @Operation(summary = "查询充电宝列表")
    @GetMapping("/list")
    public TableDataInfo list(PowerBank powerBank) {
        //开始分页
        startPage();
        //查询数据库
        List<PowerBank> list = powerBankService.selectPowerBankList(powerBank);
        TableDataInfo dataInfo = getDataTable(list);
        return dataInfo;
    }

}
